{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting binarytree\n",
      "  Using cached binarytree-5.1.0-py2.py3-none-any.whl (14 kB)\n",
      "Installing collected packages: binarytree\n",
      "Successfully installed binarytree-5.1.0\n"
     ]
    }
   ],
   "source": [
    "!pip install binarytree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "       12______\n",
      "      /        \\\n",
      "     8        __14\n",
      "    /        /    \\\n",
      "  _4        9      0\n",
      " /         / \\      \\\n",
      "10        6   7      2\n",
      "\n",
      "[Node(12)]\n",
      "[Node(8), Node(14)]\n",
      "[Node(4), Node(9), Node(0)]\n",
      "[Node(10), Node(6), Node(7), Node(2)]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[12], [14, 8], [4, 9, 0], [2, 7, 6, 10]]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from binarytree import tree\n",
    "\n",
    "class Solution:\n",
    "    def main(self, root) -> int:\n",
    "        levels = self.travel(root)\n",
    "        levels = [l if i%2==0 else list(reversed(l)) for i,l in enumerate(levels)]\n",
    "        return levels[:-1]\n",
    "        \n",
    "    def travel(self, root):\n",
    "        list_of_vals = []\n",
    "        level_list = [[root]]\n",
    "        real_level_list = [[root.val]]\n",
    "        \n",
    "        while True:\n",
    "            current_level = level_list[-1]\n",
    "            print(current_level)\n",
    "            level_list.append([])\n",
    "            real_level_list.append([])\n",
    "            i = 0\n",
    "            while i < len(current_level):\n",
    "                node = current_level[i]\n",
    "\n",
    "                list_of_vals.append(node.val)\n",
    "\n",
    "                if node.left:\n",
    "                    level_list[-1].append(node.left)\n",
    "                    real_level_list[-1].append(node.left.val)\n",
    "\n",
    "                if node.right:\n",
    "                    level_list[-1].append(node.right)\n",
    "                    real_level_list[-1].append(node.right.val)\n",
    "                \n",
    "                i += 1\n",
    "            \n",
    "            if len(level_list[-1]) == 0:\n",
    "                break\n",
    "\n",
    "            list_of_vals.append(f\"__{len(level_list)}__\")\n",
    "                \n",
    "        return real_level_list\n",
    "                \n",
    "\n",
    "my_tree = tree(height=3, is_perfect=False)\n",
    "print(my_tree)\n",
    "Solution().main(my_tree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal\n",
    "\n",
    "\n",
    "\n",
    "Runtime: 28 ms, faster than 87.65% of Python3 online submissions for Binary Tree Zigzag Level Order Traversal.\n",
    "Memory Usage: 14.5 MB, less than 14.48% of Python3 online submissions for Binary Tree Zigzag Level Order Traversal.\n",
    "\n",
    "\n",
    "\n",
    "```py\n",
    "# Definition for a binary tree node.\n",
    "# class TreeNode:\n",
    "#     def __init__(self, val=0, left=None, right=None):\n",
    "#         self.val = val\n",
    "#         self.left = left\n",
    "#         self.right = right\n",
    "class Solution:\n",
    "    def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n",
    "        if root == None:\n",
    "            return []\n",
    "        levels = self.travel(root)\n",
    "        levels = [l if i%2==0 else list(reversed(l)) for i,l in enumerate(levels)]\n",
    "        return levels[:-1]\n",
    "        \n",
    "    def travel(self, root):\n",
    "        list_of_vals = []\n",
    "        level_list = [[root]]\n",
    "        real_level_list = [[root.val]]\n",
    "        \n",
    "        while True:\n",
    "            current_level = level_list[-1]\n",
    "            #print(current_level)\n",
    "            level_list.append([])\n",
    "            real_level_list.append([])\n",
    "            i = 0\n",
    "            while i < len(current_level):\n",
    "                node = current_level[i]\n",
    "\n",
    "                list_of_vals.append(node.val)\n",
    "\n",
    "                if node.left:\n",
    "                    level_list[-1].append(node.left)\n",
    "                    real_level_list[-1].append(node.left.val)\n",
    "\n",
    "                if node.right:\n",
    "                    level_list[-1].append(node.right)\n",
    "                    real_level_list[-1].append(node.right.val)\n",
    "                \n",
    "                i += 1\n",
    "            \n",
    "            if len(level_list[-1]) == 0:\n",
    "                break\n",
    "\n",
    "            list_of_vals.append(f\"__{len(level_list)}__\")\n",
    "                \n",
    "        return real_level_list\n",
    "```\n",
    "\n",
    "\n",
    "Spent 15 minutes\n",
    "\n",
    "Faild 1 time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
